
package vv.lines.core;

import vv.lines.core.context.IContext;
import vv.lines.store.GameChampion;
import vv.lines.utils.TextResources;
import vv.lines.view.View;
import vv.lines.view.WindowView;

import java.util.List;

import static vv.lines.utils.TextResources.*;

/**
 *
 * @author vpotapenko
 */
public class GameWindow extends VisualObject implements IActionHandler {

    private IActionHandler handler;
    private TextWindow aboutWindow;
    private TextWindow rulesWindow;

    public GameWindow(IActionHandler handler, IContext context) {
        super(context);
        this.handler = handler;
    }

    public void setField(Field field) {
        getView().setFieldView(field.getView());
    }

    @Override
    public WindowView getView() {
        return (WindowView) super.getView();
    }

    public void setVisible(boolean visible) {
        getView().setVisible(visible);
    }

    @Override
    protected View createView() {
        WindowView view = getViewFactory().createWindowView(this);
        view.setTitle(TITLE);

        return view;
    }

    public void updateScore() {
        getView().updateScore(getContext().getScore());
    }

    public void updateNextBalls() {
        getView().updateNextBalls(getContext().getNextBalls());
    }

    public void updateChampions() {
        List<GameChampion> champions = getContext().getChampionsStore().getChampions();
        getView().updateChampions(champions);        
    }

    public void handleAction(ActionId action) {
        handler.handleAction(action);
    }

    public void showAbout() {
        checkAboutWindow();
        aboutWindow.show(getView());
    }

    public void showRules() {
        checkRulesWindow();
        rulesWindow.show(getView());
    }

    private void checkRulesWindow() {
        if (rulesWindow != null) return;

        rulesWindow = new TextWindow(GAME_RULES, getContext());
        rulesWindow.setTextResource(GAME_RULES_FILE_NAME);
        rulesWindow.setTextSize(400, 300);
    }

    private void checkAboutWindow() {
        if (aboutWindow != null) return;

        aboutWindow = new TextWindow(TextResources.ABOUT, getContext());
        aboutWindow.setTextResource(ABOUT_FILE_NAME);
        aboutWindow.setTextSize(300, 100);
    }
}
